package com.project.erp.action.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.project.erp.action.BaseAction;
import com.project.erp.common.utils.Page;
import com.project.erp.model.system.Param;
import com.project.erp.model.system.ParamValue;
import com.project.erp.service.global.GlobalResourceService;
import com.project.erp.service.system.ParamService;
import com.project.erp.service.system.ParamValueService;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司

 *  功能模块名：系统参数action类 

 *  功能描述：处理系统参数操作请求

 * @author LiJiaGuo    

 * @version 1.0  

 * @created 2013-11-10 下午3:56:46
 */
@Controller
@Scope("prototype")
public class ParamAction extends BaseAction implements ModelDriven<Param> {

	private static final long serialVersionUID = -8664016900625244424L;
	private Param paramk;
	private String paramIds;
	private ParamValue paramValue;
	private List<ParamValue> paramValueList;
	
	//细表字段
	private Long[] paramValueIds;
	private String[] paramKey;
	private String[] paramValues;
	
	@Autowired
	private ParamService paramService;
	@Autowired
	private ParamValueService paramValueService;
	@Autowired
	private GlobalResourceService globalResourceService;
	
	/**
	 * 取得系统参数列表
	 * @return
	 * @throws Exception
	 */
	public String getParamListData() throws Exception{
		logger.debug("entry getParamListData method ...");
		try{
			Page<Param> page = new Page<Param>();
			page.setPageNo(getPage());
			page.setPageSize(getRows());
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
			page = paramService.getParamPageBySearch(page, paramk);
			
			//设置表格返回数据
			this.getResultMap().put("total", page.getTotalCount());
			this.getResultMap().put("rows", page.getResult());
		}catch(Exception e){
			logger.error("execute getParamListData error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 到添加系统参数页面
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toAddParam() throws Exception{
		logger.debug("entry toAddParam method!...");
		
		return SUCCESS;
	}
	
	/**
	 * 保存系统参数信息
	 * @return SUCCESS
	 * @throws Exception 异常信息
	 */
	public String addParam() throws Exception{
		logger.debug("entry addParam method!...");

		try{
			paramValueList = new ArrayList<ParamValue>();
			if(paramKey != null){
				//设置细表信息
				for(int i=0;i<paramKey.length;i++){
					paramValue = new ParamValue();
					
					paramValue.setParamKey(paramKey[i]);
					paramValue.setParamValue(paramValues[i]);
					paramValue.setParam(paramk);
					paramValueList.add(paramValue);
				}
				paramk.setParamList(paramValueList);
			}
			paramService.saveParam(paramk);
			
			//重新加载系统参数到application
			globalResourceService.loadParameter(getSession().getServletContext());
			
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			this.getResultMap().put("msg","添加系统参数信息成功");
		}catch(Exception e){
			//返回错误信息
			logger.error("添加系统参数信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
			this.getResultMap().put("msg","添加系统参数信息失败");
		}
		return SUCCESS;
	}
	
	/**
	 * 到修改系统参数页面
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toEditParam() throws Exception{
		logger.debug("entry toEditParam method!...");
		try{
			logger.info("********************paramId="+paramk.getId());
			Long paramId = paramk.getId();
			paramk = paramService.getParamById(paramId);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("execute toEditParam page error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 修改系统参数信息
	 * @return
	 * @throws Exception
	 */
	public String editParam() throws Exception{
		logger.debug("entry editParam method!...");
		try{
			logger.info("param="+paramk);
			Param persistentParam = paramService.getParamById(paramk.getId());
			
			paramValueList = new ArrayList<ParamValue>();
			List<ParamValue> delParaValueList = persistentParam.getParamList();
			
			if(paramKey != null){//针对没有细表记录的情况
				//设置细表信息
				for(int i=0;i<paramKey.length;i++){
					//新增的记录
					if((paramValueIds[i] == null) || ("".equals(paramValueIds[i]))){
						paramValue = new ParamValue();
						
						paramValue.setParamKey(paramKey[i]);
						paramValue.setParamValue(paramValues[i]);
						paramValue.setParam(paramk);
						paramValueList.add(paramValue);
					}else{//修改的记录
						ParamValue persistentParamValue = paramValueService.getParamValueById(paramValueIds[i]);

						persistentParamValue.setParamKey(paramKey[i]);
						persistentParamValue.setParamValue(paramValues[i]);
						persistentParamValue.setParam(persistentParam);

						paramValueList.add(persistentParamValue);
					}
				}
				
				paramk.setParamList(paramValueList);
			}else{
				persistentParam.setParamList(null);
			}

			BeanUtils.copyProperties(paramk, persistentParam, new String[]{});
			//修改系统参数信息
			paramService.editParam(persistentParam,delParaValueList,paramValueIds);
			
			//重新加载系统参数到application
			globalResourceService.loadParameter(getSession().getServletContext());
			
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			this.getResultMap().put("msg", "修改系统参数信息成功");
		}catch(Exception e){
			//返回错误信息
			logger.error("修改系统参数信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
			this.getResultMap().put("msg", "修改系统参数信息失败");
		}
		return SUCCESS;
	}
	
	/**
	 * 删除系统参数信息
	 * @return
	 * @throws Exception
	 */
	public String delParam() throws Exception{
		logger.debug("entry delParam method!...");
		try{
			logger.info("paramIds="+paramIds);
			String[] arrayIds = paramIds.split(",");
			paramService.delParam(arrayIds);
			
			//重新加载系统参数到application
			globalResourceService.loadParameter(getSession().getServletContext());
			
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			this.getResultMap().put("msg", "删除系统参数成功");
		}catch(Exception e){
			//返回错误信息
			e.printStackTrace();
			this.getResultMap().put("result",ERROR);
			this.getResultMap().put("msg", "删除系统参数失败");
		}
		return SUCCESS;
	}
	
	public String checkParam() throws Exception{
		logger.debug("entry checkParam method!...");
		try{
			this.getResultMap().put("checkFlag",true);
			if(StringUtils.isNotBlank(paramk.getNameEn())){
				Param par = paramService.getParamByNameEn(paramk.getNameEn());
				if(null != par){
					this.getResultMap().put("checkFlag",false);
				}
			}
		}catch(Exception e){
			//返回错误信息
			logger.error("entry checkParam method exception:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
		}
		this.getResultMap().put("result",SUCCESS);
		return SUCCESS;
	}
	
	@Override
	public Param getModel() {
		if(null == paramk){
			paramk = new Param();
		}
		return paramk;
	}

	public Param getParamk() {
		return paramk;
	}

	public void setParamk(Param paramk) {
		this.paramk = paramk;
	}

	public Long[] getParamValueIds() {
		return paramValueIds;
	}

	public void setParamValueIds(Long[] paramValueIds) {
		this.paramValueIds = paramValueIds;
	}

	public String[] getParamKey() {
		return paramKey;
	}

	public void setParamKey(String[] paramKey) {
		this.paramKey = paramKey;
	}

	public String[] getParamValues() {
		return paramValues;
	}

	public void setParamValues(String[] paramValues) {
		this.paramValues = paramValues;
	}

	public String getParamIds() {
		return paramIds;
	}

	public void setParamIds(String paramIds) {
		this.paramIds = paramIds;
	}

	public ParamValue getParamValue() {
		return paramValue;
	}

	public void setParamValue(ParamValue paramValue) {
		this.paramValue = paramValue;
	}

	public List<ParamValue> getParamValueList() {
		return paramValueList;
	}

	public void setParamValueList(List<ParamValue> paramValueList) {
		this.paramValueList = paramValueList;
	}

}
